function N= func_wrapper_main(principal_data,teacher_data,store_suffix ,rc_on,year, N      )

%% load principal prefs and clean them up


  
%rc_on=1
  

% this works: for a full string
folder='../../01_data/05_temp/';
fullmatfilename=fullfile(folder, principal_data);
load(fullmatfilename)
 

%load('../../01_data/05_temp/mixed_logit_principal_totVA_wkdonut')

   
    IJX_princ=[ ones(length(IJX_princ),1) IJX_princ];   % add a vector of ones  
    IJ_princ=IJX_princ * thetahat(1:end-1,1);     % don't include RC
    clearvars -except IJ_princ IJN* IJVA* principal_data teacher_data store_suffix N rc_on year

    
folder='../../01_data/05_temp/';
fullmatfilename=fullfile(folder, teacher_data);
load(fullmatfilename)


%load('../../01_data/05_temp/mixed_logit_rc6_totVA_day1')

 
if rc_on==1
    % select the best Xdraws and clean them up
    [Xdraws_best] = func_likely_draws_current(IJitidx, IJchoiceset,IJcurrentFirst, Xdraws,Xmat,thetahat,y_logistic);
    
    % redo Xdraws_best to be as long as everything else
    [Ci, IAi, ICi]=unique(IJitidx(IJchoiceset==1));
    Xdraws_unique=Xdraws_best(IAi,:);
    Xdraws_best2=Xdraws_unique(IJitidx,:);
    
    % find predicted values in each match (teacher preferences)
    
    IJipref=[Xmat Xdraws_best2]*thetahat;
end

if rc_on==0
    
    IJipref=xb;
end
    
    % generate IJpref that is -999999999 if choiceset=0
    
    IJipref_choice=IJchoiceset.*IJipref + (1-IJchoiceset)*(-9999999999);
    
    % generate IJfracDisadv that is -99999999 if choicset=0
    
    IJfracDisadv_choice=IJchoiceset.*IJfracDisadv + (1-IJchoiceset)*(-9999999999);
 
    IJNdis= IJclassSize.*IJfracDisadv;
    IJNdis_choice=IJchoiceset.*IJNdis + (1-IJchoiceset)*(-9999999999);

    
    % build school preferences (by changing this block, can make it principal preferences, or....)
      
    IJoutput=IJclassSize.*IJVAma;
    IJoutput_dis=IJclassSize.*IJfracDisadv.*IJVAm2;
    IJoutput_adv=IJclassSize.*(1-IJfracDisadv).*IJVAm1;
    
  % generate ouput that is -999999999 if choiceset=0
    
    IJoutput_choice=IJchoiceset.*IJoutput + (1-IJchoiceset)*(-9999999999);   
    IJoutput_dis_choice=IJchoiceset.*IJoutput_dis + (1-IJchoiceset)*(-9999999999);   
    IJoutput_adv_choice=IJchoiceset.*IJoutput_adv + (1-IJchoiceset)*(-9999999999);   

    % generate IJ_princ that is -99999 if choiceset=0

    IJ_princ_choice=IJchoiceset.*IJ_princ + (1-IJchoiceset)*(-9999999999);  
    IJ_choice= (1-IJchoiceset)*(-9999999999);  

    % generate principal for non-title I, and VA for title I
    
    IJ_princ_ntitleI_va_titleI=IJ_princ.*(IJfracDisadv<0.4) + IJoutput.*(IJfracDisadv>0.4)+IJoutput.*(IJfracDisadv==0.4);
    IJ_princ_titleI_va_ntitleI=IJ_princ.*(IJfracDisadv>0.4) + IJoutput.*(IJfracDisadv<0.4)+IJ_princ.*(IJfracDisadv==0.4);

    % generate 4 variables from the x matrix
    
    IJblack=Xmat(:,30);
    IJexp23=Xmat(:,33);
    IJexp46=Xmat(:,34);
    IJexp7p=Xmat(:,35);
    




    %% 1. status, quo do N runs of preferences: principal pref, restricted choice sets
        
   
    stable_teach_propose= zeros(N,4);
    stable_school_propose=zeros(N,4);
    firstbest_school=zeros(N,4);   
    teach_school_same=zeros(N,1);

    
    
    for kk=1:N
   % error_draws=evrnd(0,1,length(IJitidx),1).*(IJfracDisadv>0.4);  
       error_draws=evrnd(0,1,length(IJitidx),1);       

    [stable_teach_propose(kk,:),stable_school_propose(kk,:),firstbest_school(kk,:), teach_school_same(kk,:),jass_iprop_char(:,:,kk), jass_jprop_char(:,:,kk)] = func_allocations(  IJipref,IJipref_choice,IJ_princ_choice+error_draws, IJVAma,IJclassSize,IJVAm1, IJVAm2,IJVAmean,IJfracDisadv, IJitidx,IJjtidx,IJblack,IJexp7p,IJappyear,  year,1,1,0);  
    end

status_quo=mean(stable_teach_propose(:,4));

base=['alloc_teach_est_princ_est_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'stable_school_propose','stable_teach_propose','teach_school_same','firstbest_school','jass_iprop_char','jass_jprop_char');


clearvars stable_teach_propose stable_school_propose firstbest_school teach_school_same jass_iprop_char jass_jprop_char

    %% 2. status, quo do N runs of preferences: principal noisy, restricted choice sets
        
   
    stable_teach_propose= zeros(N,4);
    stable_school_propose=zeros(N,4);
    firstbest_school=zeros(N,4);   
    teach_school_same=zeros(N,1);

    
    
    for kk=1:N
   % error_draws=evrnd(0,1,length(IJitidx),1).*(IJfracDisadv>0.4);  
       error_draws=evrnd(0,1,length(IJitidx),1);       

    [stable_teach_propose(kk,:),stable_school_propose(kk,:),firstbest_school(kk,:), teach_school_same(kk,:),jass_iprop_char(:,:,kk), jass_jprop_char(:,:,kk)] = func_allocations(  IJipref,IJipref_choice,IJ_choice+error_draws, IJVAma,IJclassSize,IJVAm1, IJVAm2,IJVAmean,IJfracDisadv, IJitidx,IJjtidx,IJblack,IJexp7p,IJappyear,  year,1,1,0);  
    end

status_quo_noisy=mean(stable_teach_propose(:,4));

base=['alloc_teach_est_princ_noisy_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'stable_school_propose','stable_teach_propose','teach_school_same','firstbest_school','jass_iprop_char','jass_jprop_char');


clearvars stable_teach_propose stable_school_propose firstbest_school teach_school_same jass_iprop_char jass_jprop_char


  % 3. all options do N runs of preferences: principal pref, unrestricted choice sets
        

    stable_teach_propose= zeros(N,4);
    stable_school_propose=zeros(N,4);
    firstbest_school=zeros(N,4);     
    teach_school_same=zeros(N,1);
    
    
    for kk=1:N
   % error_draws=evrnd(0,1,length(IJitidx),1).*(IJfracDisadv>0.4);  
       error_draws=evrnd(0,1,length(IJitidx),1);       

    [stable_teach_propose(kk,:),stable_school_propose(kk,:),firstbest_school(kk,:), teach_school_same(kk,:),jass_iprop_char(:,:,kk), jass_jprop_char(:,:,kk)] = func_allocations(  IJipref,IJipref,IJ_princ+error_draws, IJVAma,IJclassSize,IJVAm1, IJVAm2,IJVAmean,IJfracDisadv, IJitidx,IJjtidx,IJblack,IJexp7p,IJappyear,  year,1,1,0);  
    end

   % print results


all_options=mean(stable_teach_propose(:,4));


base=['alloc_teach_est_princ_est_unrestr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);

  save(savefile,'stable_school_propose','stable_teach_propose','teach_school_same','firstbest_school','jass_iprop_char','jass_jprop_char');
clearvars stable_teach_propose stable_school_propose firstbest_school teach_school_same jass_iprop_char jass_jprop_char  
 
     % 4. principal max VA, restricted choice sets do N runs of preferences: VA, restricted choice sets
        

    stable_teach_propose= zeros(N,4);
    stable_school_propose=zeros(N,4);
    firstbest_school=zeros(N,4);      
    teach_school_same=zeros(N,1);
    
    
    for kk=1:N
   % error_draws=evrnd(0,1,length(IJitidx),1).*(IJfracDisadv>0.4);  
       error_draws=evrnd(0,1,length(IJitidx),1);       

    [stable_teach_propose(kk,:),stable_school_propose(kk,:),firstbest_school(kk,:), teach_school_same(kk,:),jass_iprop_char(:,:,kk), jass_jprop_char(:,:,kk)] = func_allocations(  IJipref,IJipref_choice,IJoutput_choice, IJVAma,IJclassSize,IJVAm1, IJVAm2,IJVAmean,IJfracDisadv, IJitidx,IJjtidx,IJblack,IJexp7p,IJappyear,  year,1,1,0);  
    end
 
    
 teach_est_princ_va_restr=mean(stable_teach_propose(:,4));
   
    
base=['alloc_teach_est_princ_va_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
  save(savefile,'stable_school_propose','stable_teach_propose','teach_school_same','firstbest_school','jass_iprop_char','jass_jprop_char');
 
clearvars stable_teach_propose stable_school_propose firstbest_school teach_school_same jass_iprop_char jass_jprop_char   

      %% 5. teacher max Ndis , principal est, timing restricted
        
   
    stable_teach_propose= zeros(N,4);
    stable_school_propose=zeros(N,4);
    firstbest_school=zeros(N,4);    
    teach_school_same=zeros(N,1);
    clearvars irank_out jrank_out1 jrank_out2
    
    
    for kk=1:N
   % error_draws=evrnd(0,1,length(IJitidx),1).*(IJfracDisadv>0.4);  
       error_draws=evrnd(0,1,length(IJitidx),1);       

    [stable_teach_propose(kk,:),stable_school_propose(kk,:),firstbest_school(kk,:), teach_school_same(kk,:),jass_iprop_char(:,:,kk), jass_jprop_char(:,:,kk)] = func_allocations(  IJipref,IJNdis_choice,IJ_princ_choice+error_draws, IJVAma,IJclassSize,IJVAm1, IJVAm2,IJVAmean,IJfracDisadv, IJitidx,IJjtidx,IJblack,IJexp7p,IJappyear,2015,1,0,0);  
    end


teach_ndis_princ_est_restr=mean(stable_teach_propose(:,4));

base=['alloc_teach_ndis_princ_est_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'stable_school_propose','stable_teach_propose','teach_school_same','firstbest_school','jass_iprop_char','jass_jprop_char');

clearvars stable_teach_propose stable_school_propose firstbest_school teach_school_same jass_iprop_char jass_jprop_char


      %% 6. teacher max N dis, principal VA max, timing restricted
        
   
    stable_teach_propose= zeros(N,4);
    stable_school_propose=zeros(N,4);
    firstbest_school=zeros(N,4);   
    teach_school_same=zeros(N,1);
    
    
    for kk=1:N
   % error_draws=evrnd(0,1,length(IJitidx),1).*(IJfracDisadv>0.4);  
       error_draws=evrnd(0,1,length(IJitidx),1);       

    [stable_teach_propose(kk,:),stable_school_propose(kk,:),firstbest_school(kk,:), teach_school_same(kk,:),jass_iprop_char(:,:,kk), jass_jprop_char(:,:,kk)] = func_allocations(  IJipref,IJNdis_choice,IJoutput_choice, IJVAma,IJclassSize,IJVAm1, IJVAm2,IJVAmean,IJfracDisadv, IJitidx,IJjtidx,IJblack,IJexp7p,IJappyear,  2015,1,0,0);  
    end


teach_ndis_princ_va_restr=mean(stable_teach_propose(:,4));

base=['alloc_teach_ndis_princ_va_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'stable_school_propose','stable_teach_propose','teach_school_same','firstbest_school','jass_iprop_char','jass_jprop_char');

clearvars stable_teach_propose stable_school_propose firstbest_school teach_school_same jass_iprop_char jass_jprop_char 





% 7. for first best, need principal maximizing VA of dis, and unrestricted choice
% sets

   stable_teach_propose= zeros(N,4);
    stable_school_propose=zeros(N,4);
    firstbest_school=zeros(N,4);   
    teach_school_same=zeros(N,1);
    
    
    for kk=1:N
   % error_draws=evrnd(0,1,length(IJitidx),1).*(IJfracDisadv>0.4);  
       error_draws=evrnd(0,1,length(IJitidx),1);       

       [stable_teach_propose(kk,:),stable_school_propose(kk,:),firstbest_school(kk,:), teach_school_same(kk,:),jass_iprop_char(:,:,kk), jass_jprop_char(:,:,kk)] = func_allocations(  IJipref,IJfracDisadv,IJoutput_dis, IJVAma,IJclassSize,IJVAm1, IJVAm2,IJVAmean,IJfracDisadv, IJitidx,IJjtidx,IJblack,IJexp7p,IJappyear,  2015,1,0,1);  
    end


    first_best=mean(firstbest_school(:,4));

teach_fracdis_princ_va_unrestr=mean(stable_teach_propose(:,4));

base=['alloc_teach_fracdis_princ_va_unrestr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'stable_school_propose','stable_teach_propose','teach_school_same','firstbest_school','jass_iprop_char','jass_jprop_char');


clearvars stable_teach_propose stable_school_propose  firstbest_school teach_school_same jass_iprop_char jass_jprop_char 

end 



    
 